package org.skatiger.jrove.component.core;

import java.io.IOException;

import org.skatiger.jrove.ViewContext;
import org.skatiger.jrove.component.UIComponent;
import org.skatiger.jrove.component.UIComponentBase;
import org.skatiger.jrove.util.StringUtils;

public class If extends UIComponentBase {
    private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory
            .getLogger(If.class);
    private Boolean test;
    private String var;

    public String getVar() {
        return getPropertyValue("var", var);
    }

    public void setVar(String var) {
        this.var = var;
    }

    public void setTest(Boolean test) {
        this.test = test;
    }

    public Boolean getTest() {
        return getPropertyValue("test", test);
    }

    @Override
    public boolean getRendersChildren() {
        return true;
    }

    @Override
    public void encodeChildren(ViewContext context) throws IOException {
        super.encodeChildren(context);
        Boolean test = getTest();
        if (test != null) {
            String var = getVar();
            if (StringUtils.isBlank(var)) {
                if (test) {
                    if (getChildCount() > 0) {
                        for (UIComponent child : getChildren()) {
                            if (!child.isRendered()) {
                                continue;
                            }
                            child.encodeAll(context);
                        }
                    }
                }
            } else {
                context.setAttribute(var, test);
            }
        } else {
            logger.warn("jstl.core.if tag, the test attribute was null.");
        }
    }

}
